let { goodsModel } = require("./models/goodModel");

//新增
module.exports.addGood = async function (data) {
    let res = await goodsModel.create({...data,status:0});
    return res;
};
//查询详情数据
module.exports.getDetailGood=async function(data){
    let res=await goodsModel.find(data)
    return res
}
//查询
module.exports.searchGood = async function ({
    goodName = "",
    pageSize = 10,
    pageIndex = 1,
}) {
    let res = await goodsModel.aggregate([
        {
            $lookup: {
                from: "goodType",
                localField: "goodsType",
                foreignField: "_id",
                as: "goodsType",
            },
        },
        {
            $match:{
                "goodName": {$regex: goodName, $options: '$i'}
            }
        },
        {
            $unwind: "$goodsType",
        },
        {
            $project: {
                _id: 1,
                goodName: 1,
                num: 1,
                status: 1,
                createdAt: 1,
                updatedAt: 1,
                creater: 1,
                goodsType: "$goodsType.goodsType",
                img:1
            },
        },
        {
            $skip: (pageIndex - 1) * (pageSize - 0),
        },
        {
            $limit: pageSize - 0,
        },
    ]);
    let totalNum = await goodsModel.countDocuments(
        {"goodName": {$regex: goodName, $options: '$i'}}
    );
    return { result: [...res], totalNum };
};


//更新数据
module.exports.updateDetail=async function({_id,...rest}){
    console.log(rest)
    let res=await goodsModel.updateOne({_id},{...rest})
    return res
}


//s删除
module.exports.deleteGood=async(data)=>{
    let res=await goodsModel.deleteOne(data)
    return res
}
